﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;

namespace Ornithopter.FlashDiskHelper
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<LogicalDisk> LogicalDisks = new List<LogicalDisk>();

        public MainWindow()
        {
            InitializeComponent();
            this.MouseMove += new MouseEventHandler(MainWindow_MouseMove);
            this.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - 300;
            this.Top = 80;
        }

        void MainWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            USBStorage.StartListenning();

            USBStorage.USBStorageAttached += new Action<USBStorage>(USBStorage_USBStorageAttached);
            USBStorage.USBStorageRemoved += new Action<USBStorage>(USBStorage_USBStorageRemoved);
        }

        void USBStorage_USBStorageRemoved(USBStorage obj)
        {
            RemoveDevice(obj);
        }

        private void AddDevice(USBStorage device)
        {
            foreach (var logicalDisk in device.LogicalDisks)
	        {
                LogicalDisks.Add(logicalDisk);
                this.listBox1.Items.Add(new ItemView1(logicalDisk));
	        }
            if (this.listBox1.Items.Count > 0)
            {
                this.Opacity = 1.0;
            }
        }

        private void RemoveDevice(USBStorage device)
        {
            foreach (var logicalDisk in device.LogicalDisks)
            {
                foreach (var item in this.listBox1.Items)
                {
                    if (((ItemView1)item).LogicalDisk.OwnerDevice.Id == logicalDisk.OwnerDevice.Id)
                    {
                        ((ItemViewControlBase)item).Close(() =>
                            {
                                LogicalDisks.Remove(logicalDisk);
                                this.listBox1.Items.Remove(item);
                                if (this.listBox1.Items.Count == 0)
                                {
                                    this.Opacity = 0.0;
                                }
                                logicalDisk.Dispose();
                                ((ItemViewControlBase)item).Dispose();
                                GC.Collect();
                            });
                        break;
                    }
                }
            }
            
        }
        void USBStorage_USBStorageAttached(USBStorage obj)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    AddDevice(obj);
                }));
            
        }
    }
}
